import { asyncRoutes, constantRoutes } from '@/router'
// import Layout from '@/layout'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

export const loadView = (view) => {
  return (resolve) => require([`@/views${view}`], resolve)
}

// export function generaMenu(routes, data) {
//   console.log('data', data)
//   data.forEach(item => {
//   // 这里的字段在实际业务开发中都有用的，当然如果只是简单学习的话，id，label可以移除
//     const menu = {
//       id: item.id,
//       label: item.label,
//       name: item.name,
//       path: item.path === '#' ? item.id + '_key' : item.path,
//       component: item.component === 'Layout' ? Layout : loadView(item.component),
//       redirect: item.redirect,
//       hidden: item.hidden,
//       alwaysShow: item.alwaysShow,
//       meta: item.meta,
//       children: []
//     }
//     if (item.children) {
//       generaMenu(menu.children, item.children)
//     }
//     routes.push(menu)
//   })
//   console.log(routes)
// }

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes({ commit }, roles) {
    return new Promise(resolve => {
      let accessedRoutes
      // if (roles.includes('admin')) {
      // eslint-disable-next-line prefer-const
      accessedRoutes = asyncRoutes || []
      // } else {
      // accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
      // }
      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
    })
  }
}
// https://blog.csdn.net/weixin_44922964/article/details/120927244

// const actions = {
//   generateRoutes({ commit }, roles) {
//     return new Promise(resolve => {
//       const loadMenuData = []
//       // 先查询后台并返回左侧菜单数据并把数据添加到路由
//       const data = [
//       ]

//       // fetchAppoint().then(res => {
//       //   console.log(res)
//       // }).catch(error => {
//       //   console.log(error)
//       // })

//       Object.assign(loadMenuData, data)
//       const tempAsyncRoutes = Object.assign([], asyncRoutes)
//       // tempAsyncRoutes = asyncRoutes
//       generaMenu(tempAsyncRoutes, loadMenuData)
//       var accessedRoutes

//       console.log('tempAsyncRoutes', tempAsyncRoutes)
//       accessedRoutes = tempAsyncRoutes || []
//       console.log('accessedRoutes', accessedRoutes)

//       commit('SET_ROUTES', accessedRoutes)
//       resolve(accessedRoutes)
//     })
//   }
// }

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
